﻿using System;
using System.Collections.Generic;

namespace ircBot
{

    public class MessageBuffer<T>
    {


        protected int mCapacity = int.MaxValue;
        protected readonly List<T> buffer = new List<T>();

        public void Add(T item)
        {
            this.buffer.Add(item);
            
            while (this.Count > this.mCapacity)
            {
                this.buffer.RemoveAt(0);
            }
        
        }

        public void Remove()
        {
            if (this.Count > 0) this.buffer.RemoveAt(0);
        }

        public T FindLast(Predicate<T> match)
        {

            return this.buffer.FindLast(match);

        }

        public bool Exists(Predicate<T> match)
        {

            return this.buffer.Exists(match);
        
        }
        
        public int Count
        {
            get { return this.buffer.Count; }
        }

        public int Capacity
        {

            get
            {
                return this.mCapacity;
            }
            set
            {
                if (value <= 0)
                {
                    throw new ArgumentOutOfRangeException();
                }
                this.mCapacity = value;
                while (this.Count > this.mCapacity)
                {
                    this.Remove();
                }
            }

        }

    }

}